/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
    ListNode *removeNthFromEnd(ListNode *head, int n)
    {
        ListNode *dummy = new ListNode(0, head);
        ListNode *first = dummy;
        ListNode *second = head;
        for (int i = 0; i < n; i++)
        {
            second = second->next;
        }
        while (second != nullptr)
        {
            second = second->next;
            first = first->next;
        }
        first->next = first->next->next;
        second = dummy->next;
        delete dummy;
        return second;
    }
};

int main(int argc, char const *argv[])
{
    ListNode *head = new ListNode(1);
    // head->next = new ListNode(1);
    Solution().removeNthFromEnd(head, 1);
    return 0;
}
